#include "I2C_eUSCI_B1_TMP75.h"

// ==========================================================================
// I2C 接口 TMP75 数字温度传感器与微控制器连线
// MSP432P401R 引脚                 TMP75 引脚
// P6.5/UCB1SOMI/UCB1SCL/C1.2       TMP75 SCL       I2C 时钟线
// P6.4/UCB1SIMO/UCB1SDA/C1.3       TMP75 SDA       I2C 数据线
// ==========================================================================
// 初始化 UCB1 I2C 功能
void I2C_Init_TMP75(void)
{
    // ------------------------------------------------
    P6->SEL0 |= BIT5 | BIT4;        // 配置 I2C 引脚
    // 设置 EUSCI_B1 工作在 I2C 模式
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_SWRST; // Software reset enabled
    EUSCI_B1->CTLW0 = EUSCI_B_CTLW0_SWRST | // Remain eUSCI in reset state
            EUSCI_B_CTLW0_MODE_3 |          // I2C mode
            EUSCI_B_CTLW0_MST |             // Master mode
            EUSCI_B_CTLW0_SYNC |            // Sync mode
            EUSCI_B_CTLW0_SSEL__SMCLK;      // SMCLK
    EUSCI_B1->BRW = 30;                     // baudrate = SMCLK / 30 = 100kHz
    EUSCI_B1->I2CSA = I2C_ADDR_TMP75;       // Slave address
    EUSCI_B1->CTLW0 &= ~EUSCI_B_CTLW0_SWRST;// Release eUSCI from reset
}

// ==========================================================================
// TMP75 温度分辨率设置
void TMP75_Res(uint8_t Res)
{
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    while (EUSCI_B1->CTLW0 & EUSCI_B_CTLW0_TXSTP); // 确认 STOP 条件已执行完
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_TR |       // I2C 切换为 TX 模式
                       EUSCI_B_CTLW0_TXSTT;     // I2C 产生 Start 信号
    // ----------------------------------------------------
    // 等待发出: 地址字节
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_TXIFG0));
    
    // 等待发数据完成
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    // 写发送寄存器, 发第 1 个字节
    EUSCI_B1->TXBUF = 0x01;     // P1:P0=01, 读/写配置寄存器
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_TXIFG0));
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    // 写发送寄存器, 发第 2 个字节
    EUSCI_B1->TXBUF = Res;     // 温度分辨率
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_TXIFG0));
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    // ----------------------------------------------------
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_TXSTP;     // I2C 产生 STOP 信号
}

// ==========================================================================
// TMP75 读温度寄存器
uint16_t TMP75_Temp_REG(void)
{
    uint16_t Rtn_Tmp;
    uint8_t RXDataH,RXDataL;    // 保存温度寄存器返回值
    // ----------------------------------------------------
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    // Ensure STOP condition got sent
    while (EUSCI_B1->CTLW0 & EUSCI_B_CTLW0_TXSTP); // 确认 STOP 条件已执行完
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_TR |       // I2C 切换为 TX 模式
                       EUSCI_B_CTLW0_TXSTT;     // I2C 产生 Start 信号
    // ----------------------------------------------------
    // 等待发出: 地址字节
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_TXIFG0));
    
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_TXIFG0;
    EUSCI_B1->TXBUF = 0x00;   // P1:P0=00, 选择温度寄存器
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_TXIFG0));// 等待发出: 数据
    // ====================================================
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_RXIFG0;
    EUSCI_B1->CTLW0 &= ~ EUSCI_B_CTLW0_TR;      // I2C 切换为 RX 模式
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_TXSTT;     // I2C 产生 START 信号
    // ----------------------------------------------------
    // 等待接收: 数据 1
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_RXIFG0));
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_RXIFG0;
    RXDataH = EUSCI_B1->RXBUF;  // 读接收寄存器, 获取1个字节
    // ----------------------------------------------------
    EUSCI_B1->CTLW0 |= EUSCI_B_CTLW0_TXSTP;     // I2C 产生 STOP 信号
    // 等待接收: 数据 2
    while (!(EUSCI_B1->IFG & EUSCI_B_IFG_RXIFG0));
    EUSCI_B1->IFG &= ~ EUSCI_B_IFG_RXIFG0;
    RXDataL = EUSCI_B1->RXBUF;  // 读接收寄存器, 获取1个字节
    // ----------------------------------------------------
    Rtn_Tmp = (RXDataH << 8) + RXDataL;
    // ----------------------------------------------------
    return Rtn_Tmp;
}
// ==========================================================================
// End of file.

